<template>
  <div>
    <el-table :data="tableData" stripe style="width: 100%" max-height="600">
      <el-table-column fixed prop="orderBy" label="序号" width="60">
        <template slot-scope="scope">
          {{ scope.row.orderBy || '1' }}
        </template>
      </el-table-column>
      <el-table-column
        prop="name"
        label="名称"
        show-overflow-tooltip
      ></el-table-column>
      <el-table-column prop="code" label="课程编码" show-overflow-tooltip>
        <template slot-scope="scope">
          {{ scope.row.code || '--' }}
        </template>
      </el-table-column>
      <el-table-column
        prop="deptName"
        label="责任单位名称"
        show-overflow-tooltip
      >
        <template slot-scope="scope">
          {{ scope.row.deptName || '--' }}
        </template>
      </el-table-column>
      <el-table-column
        prop="deptCode"
        label="责任单位编号"
        show-overflow-tooltip
      >
        <template slot-scope="scope">
          {{ scope.row.deptCode || '--' }}
        </template>
      </el-table-column>
      <el-table-column
        prop="sysTrainingLevel"
        label="培训层次"
        show-overflow-tooltip
      >
        <template slot-scope="scope">
          {{
            scope.row.hasOwnProperty('sysTrainingLevel')
              ? scope.row.sysTrainingLevel.name
              : '--'
          }}
        </template>
      </el-table-column>
      <!-- <el-table-column prop="eduMajors" label="开设专业" show-overflow-tooltip>
        <template slot-scope="scope">
          {{ courseSpecialFilter(scope.row) }}
        </template>
      </el-table-column> -->
      <el-table-column prop="createTime" label="创建时间" width="180">
      </el-table-column>
      <el-table-column label="操作" width="140">
        <template slot-scope="{ row }">
          <el-button-group>
            <el-button
              type="primary"
              size="mini"
              @click="handleEdit(row)"
              :disabled="$button('775') == false ? true : false"
              >编辑</el-button
            >
            <el-button
              type="danger"
              size="mini"
              @click="handleDelete(row)"
              :disabled="$button('776') == false ? true : false"
              >删除</el-button
            >
          </el-button-group>
        </template>
      </el-table-column>
      <div slot="empty" class="emptyBg">
        <el-empty :image-size="200"></el-empty>
      </div>
    </el-table>
    <Pagination
      v-show="parseInt(pageObj.count) > 0"
      :total="parseInt(pageObj.count)"
      @change="getList"
      :page.sync="listQuery.page"
      :limit.sync="listQuery.limit"
    />
  </div>
</template>
<script>
import {
  sysCourseSourceQuery,
  sysCourseSourceDelete,
} from '@/api/admin/courseLibrary/sysCourseSource'
import Bus from '@/utils/bus.js'
export default {
  data() {
    return {
      listQuery: {
        page: 1,
        limit: 10,
        scope: '',
      },
      tableData: [],
      pageObj: {},
    }
  },
  watch: {
    scope(v) {
      this.listQuery.scope = v
      this.getList()
    },
  },
  props: ['scope'],
  mounted() {
    this.listQuery.scope = this.scope
    this.getList()
    Bus.$on('refreshCourseSourceList', (v) => {
      if (v) {
        Object.assign(this.listQuery, v)
      }
      this.getList()
    })
  },
  beforeDestroy() {
    Bus.$off('refreshCourseSourceList')
  },
  methods: {
    getList() {
      sysCourseSourceQuery(this.listQuery).then((res) => {
        this.tableData = res.data.data
        this.pageObj = res.data
      })
    },
    //编辑
    handleEdit(row) {
      Bus.$emit('showDialogCourseSourceAdd', row)
    },
    //删除确认
    handleDelete(row) {
      this.$confirm('确定要删除吗?', '提示', {
        confirmButtonText: '删除',
        cancelButtonText: '取消',
        type: 'warning',
      })
        .then(() => {
          this.delete(row)
        })
        .catch(() => {})
    },
    //删除
    delete(row) {
      sysCourseSourceDelete(row.id).then((res) => {
        if (res.data.msg == '已引用') {
          this.$message({
            message: '该课程已被引用，无法删除',
            type: 'warning',
          })
        } else {
          this.getList()
          this.$message({
            message: '操作成功',
            type: 'success',
          })
        }
      })
    },
    courseSpecialFilter({ courseSpecialList }) {
      if (courseSpecialList && courseSpecialList.length) {
        return courseSpecialList.map((v) => v.name).toString()
      } else {
        return '--'
      }
    },
  },
}
</script>